<?php

// 本机的ip
// 通过命令行运行 cli
$server = new swoole_websocket_server("192.168.101.132", 9501);

// 握手成功回调
$server->on('open', function(swoole_websocket_server $server, $request)
{
    echo "server: handshake success with fd{$request->fd}\n";
});

// 消息监听
$server->on('message', function(swoole_websocket_server $server, $frame)
{
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    $server->push($frame->fd, "this is server {$frame->data}");
});

// 关闭监听
$server->on('close', function($ser, $fd)
{
    echo "client {$fd} closed\n";
});

// http请求监听, 如果监听了Request则可以通过http访问
// 通过 http://192.168.101.128:9501 访问
$server->on('Request', function($req, $respone)
{
    // $req->get, $req->post
    if(!empty($req->get))
    {
        // 使用全局变量
        global $server;

        // 服务器推送消息给客户端
        // 参数1 socketID, 参数2 推送内容
        $server->push(1, "this is server hello request {$req->get['msg']}");

        // 响应完成
        $respone->end("success");
    }
});

// 开始
$server->start();

?>